#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#
from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU
from base.Sequence import Sequence


class MainSequence(Sequence):
    def generate(self, **kargs):
        random_instructions = [
            "ADDW##CALM",
            "SRLI#RV64I#CALM",
            "ADDI##CALM",
            "SLLI#RV64I#CALM",
            "LUI##CALM",
        ]
        ldstr_instructions = ["LD##CALM", "SD##CALM"]
        addr_size = 48
        alignment = 8

        if self.getGlobalState("AppRegisterWidth") == 32:
            random_instructions = [
                "ADD##CALM",
                "SRLI#RV32I#CALM",
                "ADDI##CALM",
                "SLLI#RV32I#CALM",
                "LUI##CALM",
            ]
            ldstr_instructions = ["LW##CALM", "SW##CALM"]
            addr_size = 32
            alignment = 4

        for _ in range(10):
            for _ in range(self.random32(0, 5)):
                self.genInstruction(self.choice(random_instructions))

            (opt_value, opt_valid) = self.getOption("FlatMap")

            rand_VA = 0
            if opt_valid:
                rand_VA = self.genVA(
                    Size=addr_size,
                    Align=alignment,
                    Type="D",
                    Bank="Default",
                    FlatMap=opt_value,
                )
            else:
                rand_VA = self.genVA(Size=addr_size, Align=alignment, Type="D", Bank="Default")

            self.notice("gen target VA={:#x}".format(rand_VA))
            self.genInstruction(self.choice(ldstr_instructions), {"LSTarget": rand_VA})


MainSequenceClass = MainSequence
GenThreadClass = GenThreadGPGPU
EnvClass = EnvGPGPU
